﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.GetRepair
{
    public partial class GetRepairDetailEdit1 : System.Web.UI.Page
    {
        public string ID
        {
            set
            {
                ViewState["ID"] = value;
            }
            get
            {
                return ViewState["ID"] != null ? ViewState["ID"].ToString() : Guid.NewGuid().ToString();
            }
        }

        public string GetRepairID
        {
            set
            {
                ViewState["GetRepairID"] = value;
            }
            get
            {
                return ViewState["GetRepairID"] != null ? ViewState["GetRepairID"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ID = Request.QueryString["id"];
                this.GetRepairID = Request.QueryString["GetRepairID"];
                InitControlData(this.ID);
            }
        }

        private void InitControlData(string id)
        {
            string sql = @"SELECT * FROM dbo.t5715_GetRepairDetail WHERE ID = @ID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        txt1.Text = reader["ItemCode"] != null ? reader["ItemCode"].ToString() : string.Empty;
                        txt2.Text = reader["ItemName"] != null ? reader["ItemName"].ToString() : string.Empty;
                        txt3.Text = reader["MissileCode"] != null ? reader["MissileCode"].ToString() : string.Empty;
                        txt4.Text = reader["Unit"] != null ? reader["Unit"].ToString() : string.Empty;
                        txt5.Text = reader["Count"] != null ? reader["Count"].ToString() : string.Empty;
                        txt6.Text = reader["RepairUnit"] != null ? reader["RepairUnit"].ToString() : string.Empty;
                        txt7.Text = reader["InDate"] != null ? reader["InDate"].ToString() : string.Empty;
                        txt8.Text = reader["Remark"] != null ? reader["Remark"].ToString() : string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpdateData(string id, string getRepairID, string dispatchMissileID, string txt1, string txt2, string txt3, string txt4
            , string txt5, string txt6, string txt7, string txt8)
        {
            string sql_select = @"SELECT COUNT(ID) FROM dbo.t5715_GetRepairDetail WHERE ID = @ID";
            string sql_insert = @"INSERT INTO dbo.t5715_GetRepairDetail
                                       (ID
                                        ,GetRepairID
                                        ,DispatchMissileID
                                        ,ItemCode
                                        ,ItemName
                                        ,MissileCode
                                        ,Unit
                                        ,Count
                                        ,RepairUnit
                                        ,InDate
                                        ,Remark)
                                 VALUES
                                       (@ID
                                        ,@GetRepairID
                                        ,@DispatchMissileID
                                        ,@ItemCode
                                        ,@ItemName
                                        ,@MissileCode
                                        ,@Unit
                                        ,@Count
                                        ,@RepairUnit
                                        ,@InDate
                                        ,@Remark)";
            string sql_update = @"UPDATE dbo.t5715_RemoveItems
                                   SET GetRepairID = @GetRepairID
                                        ,DispatchMissileID = @DispatchMissileID
                                        ,ItemCode = @ItemCode
                                        ,ItemName = @ItemName
                                        ,MissileCode = @MissileCode
                                        ,Unit = @Unit
                                        ,Count = @Count
                                        ,RepairUnit = @RepairUnit
                                        ,InDate = @InDate
                                        ,Remark = @Remark
                                 WHERE ID = @ID";

            try
            {

                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_select);
                database.AddInParameter(dbCommand, "@ID", DbType.String, id);
                string count = database.ExecuteScalar(dbCommand).ToString();

                if (count.Equals("0"))
                {
                    dbCommand = database.GetSqlStringCommand(sql_insert);
                }
                else
                {
                    dbCommand = database.GetSqlStringCommand(sql_update);
                }

                database.AddInParameter(dbCommand, "@ID", DbType.String, id);
                database.AddInParameter(dbCommand, "@GetRepairID", DbType.String, getRepairID);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, dispatchMissileID);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, txt1);
                database.AddInParameter(dbCommand, "@ItemName", DbType.String, txt2);
                database.AddInParameter(dbCommand, "@MissileCode", DbType.String, txt3);
                database.AddInParameter(dbCommand, "@Unit", DbType.String, txt4);
                database.AddInParameter(dbCommand, "@Count", DbType.String, txt5);
                database.AddInParameter(dbCommand, "@RepairUnit", DbType.String, txt6);
                database.AddInParameter(dbCommand, "@InDate", DbType.String, txt7);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, txt8);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.GetRepairID))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
                return;
            }

            try
            {
                UpdateData(this.ID, this.GetRepairID, HiddenField8.Value
                    , HiddenField1.Value, HiddenField2.Value, HiddenField3.Value, HiddenField4.Value
                    , HiddenField5.Value, HiddenField6.Value, HiddenField7.Value, txt8.Text.Trim());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
        }
    }
}